package folder.helper;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class FileChooser {

    private JFileChooser chooser;
    public static final FileNameExtensionFilter IMAGE_FILTER = new FileNameExtensionFilter(
            "JPG & GIF & PNG & BMP Images", "jpg", "jpeg", "gif", "png", "bmp");
    public static final FileNameExtensionFilter XML_FILTER = new FileNameExtensionFilter(
            "XML", "xml");

    public FileChooser() {
        this.chooser = new JFileChooser();
        this.chooser.setAcceptAllFileFilterUsed(false);
    }

    public JFileChooser getChooser() {
        return this.chooser;
    }

    public void setDialogTitle(String titel) {
        this.chooser.setDialogTitle(titel);
    }

    public void setImageFileFilter() {
        this.chooser.addChoosableFileFilter(IMAGE_FILTER);
        this.chooser.setAccessory(new ImagePreviewAccessory(this.chooser));
    }

    public void setXmlFileFilter() {
        this.chooser.setFileFilter(new XmlFilter());
    }

    class ImageFilter extends javax.swing.filechooser.FileFilter {

        @Override
        public boolean accept(File file) {
            // Allow only directories, or files with ".txt" extension
            return file.isDirectory() || file.getAbsolutePath().endsWith(".jpg")
                    || file.getAbsolutePath().endsWith(".jpeg")
                    || file.getAbsolutePath().endsWith(".JPEG")
                    || file.getAbsolutePath().endsWith(".JPG")
                    || file.getAbsolutePath().endsWith(".jpg")
                    || file.getAbsolutePath().endsWith(".PNG")
                    || file.getAbsolutePath().endsWith(".png")
                    || file.getAbsolutePath().endsWith(".bmp")
                    || file.getAbsolutePath().endsWith(".BMP")
                    || file.getAbsolutePath().endsWith(".tiff")
                    || file.getAbsolutePath().endsWith(".TIFF");
        }

        @Override
        public String getDescription() {
            // This description will be displayed in the dialog,
            // hard-coded = ugly, should be done via I18N
            return "Image Elements (*.jpg, *.png, *.bmp, *.tiff)";
        }
    }

    class XmlFilter extends javax.swing.filechooser.FileFilter {

        @Override
        public boolean accept(File file) {
            // Allow only directories, or files with ".txt" extension
            return file.isDirectory() || file.getAbsolutePath().endsWith(".xml")
                    || file.getAbsolutePath().endsWith(".XML");
        }

        @Override
        public String getDescription() {
            // This description will be displayed in the dialog,
            // hard-coded = ugly, should be done via I18N
            return "XML documents (*.xml)";
        }
    }
}
//fileChooser.setDialogTitle("This is my open dialog");
//fileChooser.setFileFilter(new MyCustomFilter());
// Erstellung unseres FileFilters für Bilddateien
// FileFilter filter = new FileNameExtensionFilter("Bilder", 
//   "gif", "png", "jpg");         
// JFileChooser chooser = new 
//    JFileChooser("c:/programmierung/beispieldateien");
// Filter wird unserem JFileChooser hinzugefügt
       // chooser.addChoosableFileFilter(filter);